﻿Imports System.IO
Imports System.Net
Imports System.Text.RegularExpressions

Public Class Form1

    Private Sub btnParse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnParse.Click
        'Dim inStream As StreamReader
        'Dim webRequest As WebRequest
        'Dim webresponse As WebResponse
        'webRequest = webRequest.Create(txtURL.Text)
        'webresponse = webRequest.GetResponse()
        'inStream = New StreamReader(webresponse.GetResponseStream())
        'txtResult.Text = inStream.ReadToEnd()
        Dim ret As String

        lnkURL.Text = String.Empty
        txtResult.Text = String.Empty

        ListBox1.Items.Clear()

        'http://www.facebook.com/l.php?u=http%3A%2F%2Ftv7.melayutv.info%2Fvideo%2F1604&h=GAQFPjR7IAQHhqKdaCYnHy5RxcR99LHsHomU5sv09kFWYgQ
        If InStr(1, txtURL.Text, "facebook.com") And InStr(1, txtURL.Text, "melayutv.com") Or InStr(1, txtURL.Text, "melayutv.info") Then
            ret = Replace(txtURL.Text, "http://www.facebook.com/l.php?u=", "")
            ret = Replace(ret, "%3A", ":")
            ret = Replace(ret, "%2F", "/")
            txtURL.Text = ret
            Application.DoEvents()
            lnkURL.Text = "Please wait ... Generating..."
        End If

        If InStr(1, txtURL.Text, "melayutv.info") Or InStr(1, txtURL.Text, "melayutv.com") Or InStr(1, txtURL.Text, "melayutv.org") Then
            WebBrowser1.Navigate(txtURL.Text)
            lnkURL.Text = "Please wait ... Generating..."
            Application.DoEvents()

        ElseIf InStr(1, txtURL.Text, "melayu.tv") Then
            lnkURL.Text = parse_url1(txtURL.Text)
            lnkURL.Text = Replace(lnkURL.Text, "?id=yt_", "watch?v=")
            lnkURL.Text = "http://www.youtube.com/" + lnkURL.Text
            Application.DoEvents()
            lnkURL.Text = "Please wait ... Generating..."

            'https://www.facebook.com/dialog/oauth?client_id=96937694899&scope=email,publish_actions&redirect_uri=http%3A%2F%2Fwww.dailymotion.com%2Fvideo%2Fxfflen_peringatan-untuk-kita-semua-subhanallah_webcam%3Ffrom_fb_popup%3D1
            'http://www.facebook.com/l.php?u=http%3A%2F%2Fwww.dailymotion.com%2Fvideo%2Fxfflen_peringatan-untuk-kita-semua-subhanallah_webcam%3Ffb%3Dlink%26fb_action_ids%3D3393335246188%252C2973589105369%26fb_action_types%3Dvideo.watches%26fb_source%3Dother_multiline&h=HAQHgYzbHAQETiIMR5_C_K04q3FhBwnJ-hJ60eHvKtLfDHg
        ElseIf InStr(1, txtURL.Text, "dailymotion.com") Then
            ret = Replace(txtURL.Text, "%3Ffrom_fb_popup%3D1", "")
            ret = Replace(ret, "%3A", ":")
            ret = Replace(ret, "%2F", "/")
            Dim url As New System.Uri(ret)
            Dim path As String = url.Query
            ret = findurl(path)
            lnkURL.Text = "Please wait ... Generating..."
            Application.DoEvents()
            lnkURL.Text = ret
            'https://www.facebook.com/connect/uiserver.php?app_id=123277257693179&method=permissions.request&redirect_uri=http%3A%2F%2Fdigg.com%2Fnewsbar%2Fstory%2Fidentiti_sebenar_isteri_kedua_sham_kamikaze_terbongkar%3Ffb_action_ids%3D2694692661499%26fb_action_types%3Dnews.reads%26fb_source%3Dother_multiline&response_type=code&display=async&perms=email%2Cpublish_actions&auth_referral=1
        ElseIf InStr(1, txtURL.Text, "digg.com") Then
            Dim retArr() As String
            ret = Replace(txtURL.Text, "%3Ffrom_fb_popup%3D1", "")
            ret = Replace(ret, "%3A", ":")
            ret = Replace(ret, "%2F", "/")
            ret = Replace(ret, "%3F", "?")
            ret = Replace(ret, "%3D", "=")
            Dim url As New System.Uri(ret)
            Dim path As String = url.Query
            ret = findurl(path)

            retArr = ret.Split("?")

            lnkURL.Text = "Please wait ... Generating..."
            Application.DoEvents()
            lnkURL.Text = retArr(0)

            'http://www.facebook.com/l.php?u=http%3A%2F%2Fwww.zapkolik.com%2F303688%2Fdizi-cekimlerinde-yilan-korkusu-bayiltti.html&h=9AQHarOgrAQFjK2MOU-eEYwPpEe09maBClOsJoMHOD4yK8w
        ElseIf InStr(1, txtURL.Text, "zapkolik.com") Then
            Dim retArr() As String
            ret = Replace(txtURL.Text, "%3A", ":")
            ret = Replace(ret, "%2F", "/")
            'ret = Replace(ret, "%3F", "?")
            'ret = Replace(ret, "%3D", "=")
            Dim url As New System.Uri(ret)
            Dim path As String = url.Query
            ret = findurl(path)

            retArr = ret.Split("&")

            lnkURL.Text = "Please wait ... Generating..."
            Application.DoEvents()
            lnkURL.Text = retArr(0)
        Else
            lnkURL.Text = "No link provided."
            about()
        End If

        'txtResult.Text = String.Empty
        'WebBrowser1.Navigate("http://data0.net/tools/index.php")

    End Sub

    Public Function findurl(ByVal txt As String) As String
        Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&amp;\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
        Dim mactches As MatchCollection = regx.Matches(txt)

        For Each match As Match In mactches
            txt = match.Value
        Next
        
        Return txt
    End Function

    Public Function about()
        Dim a As String
        Dim b As String
        Dim c As String
        Dim d As String
        Dim why As String
        Dim why1 As String
        why = "Why do we need this software?"
        why1 = "All the video is already hosted on youtube.com right? So, why do we need another 3rd party website to browse youtube video? Those website has been design to gain your facebook access permission, popularity and cheap publicity."

        a = "What is this software doing?"
        b = "Well, actually this software help you to parse youtube link from a specific website. This will automatically by pass all the must 'share' or 'like' link in order to watch it. Make it easier for you to watch your favorite youtube video."

        c = "Apa perisian ini lakukan?"
        d = "Sebenarnya perisian ini akan membantu anda mendapatkan pautan youtube dari website tertentu. Ia akan membolehkan anda melepasi pautan 'share' atau 'like' pada website tersebut. Ini memudahkan anda menonton video kegemaran anda tanpa sebarang halangan."
        MsgBox(why & vbCrLf & why1 & vbCrLf & vbCrLf & a & vbCrLf & b & vbCrLf & vbCrLf & c & vbCrLf & d & vbCrLf & vbCrLf & "Currently we only support the following website:" & vbCrLf & vbCrLf & _
               " - www.melayu.tv" & vbCrLf & _
               " - www.melayutv.info" & vbCrLf & _
               " - www.melayutv.com" & vbCrLf & _
               " - www.melayutv.org" & vbCrLf & _
               " - www.dailymotion.com (By pass Facebook App)" & vbCrLf & _
               " - www.digg.com (By pass Facebook App)" & vbCrLf & _
               " - www.zapkolik.com (By pass Facebook App)" & vbCrLf & _
               " - or copied from facebook link." & vbCrLf & vbCrLf & _
               "If you found any bugs or want to add more website? Send your request to us @ twitter.com/alternat0r" & vbCrLf & vbCrLf & _
               "Version: " & Application.ProductVersion & " (Alpha)", MsgBoxStyle.Information)
        txtURL.Focus()
        txtURL.SelectAll()
        Return Nothing
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim lnk_tips As New ToolTip
        txtURL.Text = "Paste a URL here..."
        lnk_tips.SetToolTip(lnkURL, "Right click to copy the link. Left click to open it.")
        WebBrowser1.ScriptErrorsSuppressed = True
    End Sub


    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        'MsgBox(WebBrowser1.Document.Url)
        'If InStr(1, WebBrowser1.ToString, "data0.net") Then
        ' Exit Sub
        'End If
        If (WebBrowser1.ReadyState = WebBrowserReadyState.Complete) Then
            For Each ClientControl As HtmlElement In WebBrowser1.Document.GetElementsByTagName("meta")
                'txtResult.Text = ClientControl.GetAttribute("content")
                If (InStr(1, ClientControl.GetAttribute("content"), "youtube")) Then
                    ListBox1.Items.Add(ClientControl.GetAttribute("content"))
                End If
            Next
        End If
        If txtResult.Text = "res://ieframe.dll/bullet.png" Then
            MsgBox("The target website maybe down. Please try again.", MsgBoxStyle.Exclamation)
        End If

        'http://img.youtube.com/vi/0oGccS1w-3k/default.jpg
        lnkURL.Text = parse_url2(ListBox1.Items(0).ToString)
        lnkURL.Text = Replace(lnkURL.Text, "/vi/", "")
        lnkURL.Text = Replace(lnkURL.Text, "/hqdefault.jpg", "")
        lnkURL.Text = "http://www.youtube.com/watch?v=" + lnkURL.Text

        txtResult.Text = String.Empty
        WebBrowser1.Stop()

    End Sub

    Function parse_url1(ByVal url_path As String)
        Dim url As New System.Uri(url_path)
        Dim path As String = url.Query
        Return path
    End Function

    Function parse_url2(ByVal url_path As String)
        Dim url As New System.Uri(url_path)
        Dim path As String = url.AbsolutePath
        Return path
    End Function

    Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
        about()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        Clipboard.SetText(lnkURL.Text)
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Try
            System.Diagnostics.Process.Start(lnkURL.Text)
        Catch ex As Exception
            about()
        End Try
    End Sub

    Private Sub lnkURL_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lnkURL.MouseUp
        Try
            If e.Button = MouseButtons.Right Then
                lnkURL.ContextMenuStrip = ContextMenuStrip1
            ElseIf e.Button = MouseButtons.Left Then
                System.Diagnostics.Process.Start(lnkURL.Text)
            Else
                If lnkURL.Text = "No link provided." Or lnkURL.Text = "Please wait ... Generating..." Then
                    txtURL.Focus()
                    txtURL.SelectAll()
                End If
            End If
        Catch ex As Exception
            about()
        End Try
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub lnkURL_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkURL.LinkClicked

    End Sub
End Class
